<?php

namespace App\Http\Controllers\Tools;

use App\Http\Common\MyUtils;
use App\Http\Controllers\Controller;
use App\Models\Developer\DeveloperPackage;
use Illuminate\Http\Request;

/**
 * 权限控制器
 * Class MailController
 * @package App\Http\Controllers\Tools
 */
class SccessController extends Controller
{
    /**
     * 生成唯一的token
     * @param $request
     * @return string
     */
    public function getToken(Request $request){
        $bucket_id = $request->bucket_id;
        $bucket_secret = $request->bucket_secret;
        $count = DeveloperPackage::where(['unique_id'=>$bucket_id,'unique_key'=>$bucket_secret])->get()->count();
        if ($count){
            $expires_in = 7200;
            $code = $bucket_id.','.$bucket_secret;
            $accessToken = MyUtils::authcode($code, $operation = 'ENCODE', $key = '', $expires_in);
            $data['access_token'] = $accessToken;
            $data['create_time'] = time();
            $data['expires_in'] = $expires_in;
            return MyUtils::dealFD($data,1);
        }else{
            return MyUtils::dealFD("获取accessToken失败，请该秘钥的套件不存在，请进套件查看秘钥",-1);
        }
    }
    /**
     * 解析唯一的token
     * @param $request
     * @return string
     */
    public function parseToken(Request $request){
        if ($request->access_token){
            $expires_in = 7200;
            $code = MyUtils::authcode($request->access_token, $operation = 'DECODE', $key = '', $expires_in);
            return MyUtils::dealFD($code,1);
        }else{
            return MyUtils::dealFD("获取accessToken失败，请该秘钥的套件不存在，请进套件查看秘钥",-1);
        }
    }
}
